package com.android.project.wilfred;

import android.app.Activity;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.view.Menu;
import android.view.View;
import android.view.Menu.Item;
import android.widget.Button;

public class projectWilfred extends Activity {
    /** Called when the activity is first created. */
	
	public static final String PREFS_NAME = "WilfredPrefs";
	public static final String PREFS_THEME = "whiteonblack";
	public static final String PREFS_PUBLICMAP = "publicmap";
	public static final int RESUMEGAME = Menu.FIRST;
	public static final int OPTIONS = Menu.FIRST + 1;
	public static final int LISTSCRIPTS = Menu.FIRST + 2;
	
    @Override
    public void onCreate(Bundle icicle) {
        super.onCreate(icicle);
        loadPreferences(); //loadprefs goto's main screen on finish
    }
    
    @Override
    public void onStop() {
    	super.onStop();
    	finish();
    	/*
    	Bundle icecream = new Bundle();
    	super.onFreeze(icecream);
    	super.onStop();
    	super.onCreate(icecream);
    	loadPreferences();	*/
    }
    
    public void gotoMainScreen() {
        setContentView(R.layout.main); //TODO: Make a nice splash screen
        
        Button m_resume = (Button) findViewById(R.id.m_resume);
        m_resume.setOnClickListener(new Button.OnClickListener() {
        	public void onClick(View v) {
        		resumegame();
        	}
        });
        Button m_listscripts = (Button) findViewById(R.id.m_listscripts);
        m_listscripts.setOnClickListener(new Button.OnClickListener() {
        	public void onClick(View v) {
        		listscripts();
        	}
        });
        Button m_options = (Button) findViewById(R.id.m_options);
        m_options.setOnClickListener(new Button.OnClickListener() {
        	public void onClick(View v) {
        		setoptions();
        	}
        });
	}

	public void loadPreferences() {
    	//TODO: Keep adding to this!
    	
        final SharedPreferences settings = getSharedPreferences(PREFS_NAME, 0);
        if(settings.getBoolean(PREFS_THEME, false)) setTheme(android.R.style.Theme_Dark);
        else setTheme(android.R.style.Theme_White);
        //TODO: Change to custom colors eventually, for now just 2 themes (black-on-white, white-on-black)
        //TODO: Load publicmap preference
        
        gotoMainScreen();
	}

	public void setoptions() {    	//TODO: When hitting the back button on the options screen, it should return to the main screen, not quit the program.
        final SharedPreferences settings = getSharedPreferences(PREFS_NAME, 0);
        final SharedPreferences.Editor editor = settings.edit();
        
        setContentView(R.layout.options); //TODO: Fill options function!
        
        //This button handles switching the theme
        Button b_changeTheme = (Button) findViewById(R.id.b_changetheme);
        b_changeTheme.setOnClickListener(new Button.OnClickListener() {
            public void onClick(View v) {
            	if(settings.getBoolean(PREFS_THEME, false)) editor.putBoolean(PREFS_THEME, false);
            	else editor.putBoolean(PREFS_THEME, true);
            }
        });
        
        //This button handles weather or not you appear on the public map
        Button b_publicmap = (Button) findViewById(R.id.b_publicmap);
        b_publicmap.setOnClickListener(new Button.OnClickListener() {
            public void onClick(View v) {
            	if(settings.getBoolean(PREFS_PUBLICMAP, false)) editor.putBoolean(PREFS_PUBLICMAP, false);
            	else editor.putBoolean(PREFS_PUBLICMAP, true);
            }
        });
        
        //This button handles submitting changed options TODO: disable it if no options to be changed
        Button b_save = (Button) findViewById(R.id.b_save);
        b_save.setOnClickListener(new Button.OnClickListener() {
            public void onClick(View v) {
            	editor.commit();
            	restartWilfred();
            }
        });
        
        //This button cancels changes
        Button b_cancel = (Button) findViewById(R.id.b_cancel);
        b_cancel.setOnClickListener(new Button.OnClickListener() {
            public void onClick(View v) {
            	gotoMainScreen();
            }
        });
    }
	
	public void restartWilfred()
	{	
		setContentView(R.layout.restart);
		
		Button b_yes = (Button) findViewById(R.id.b_yes);
        b_yes.setOnClickListener(new Button.OnClickListener() {
        	public void onClick(View v) {
        		onStop();
        	}
        });
        
    	Button b_later = (Button) findViewById(R.id.b_later);
        b_later.setOnClickListener(new Button.OnClickListener() { 
        	public void onClick(View v) {
        		gotoMainScreen();
        	}
        });
	}

	@Override
    public boolean onCreateOptionsMenu(Menu menu) {
        super.onCreateOptionsMenu(menu);
        menu.add(0, RESUMEGAME, R.string.m_resume); //TODO: Show resume if game is underway, else start new game
        menu.add(0, OPTIONS, R.string.m_options);
        menu.add(0, LISTSCRIPTS, R.string.m_listscripts);
        return true;
    }
    
    @Override
    public boolean onMenuItemSelected(int featureId, Item item) {
        super.onMenuItemSelected(featureId, item);
        switch(item.getId()) {
	        case RESUMEGAME:
	            resumegame();
	            break;
	        case OPTIONS:
	            setoptions();
	            break;
	        case LISTSCRIPTS:
	        	listscripts();
        }       
        return true;
    }
    
    public void listscripts() {
    gotoMainScreen();  //TODO: Implement
    }
    
    public void resumegame () {
    gotoMainScreen();  //TODO: Implement
    }
    
}